import { RedisService } from '@common/reids/redis.service';
import { RoomService } from '../room/room.service';
import * as axios from 'axios';
import { defaultConfig } from '@game/config/config';
import { Inject, Injectable, forwardRef } from '@nestjs/common';
import { wsGlobal } from '@game/config/config';
import { HZMJService } from '../mj/hzmj.service';
import { CMMJService } from '../mj/cmmj.service';
import { WebSocketService } from '../web-socket.service';
import { MjService } from '../mj/mj.service';
import { retry } from 'rxjs';

@Injectable()
export class RebotService {
  constructor(
    public readonly roomService: RoomService,
    public readonly redisService: RedisService,
    @Inject(forwardRef(() => HZMJService))
    public readonly hzmjService: HZMJService,
    @Inject(forwardRef(() => CMMJService))
    public readonly cmmjService: CMMJService,
    @Inject(forwardRef(() => MjService))
    public readonly mjService: MjService,
    @Inject(forwardRef(() => WebSocketService))
    public readonly webSocketService: WebSocketService,
  ) {}

  async generateRobot(roomInfo,rebotNum) {
    //机器人干活 凑两人的 把接口理顺
    for (let index = 0; index < rebotNum; index++) {
      const data = await this.joinRoom(roomInfo.roomId, 'robot');
      roomInfo.users.push(data);
    }
    return roomInfo;
  }

  async begin(roomInfo) {
    let game = null;
    if(roomInfo.model==1){
        game = await this.cmmjService.begin(roomInfo);
    }else if(roomInfo.model==2){
      game = await this.hzmjService.begin(roomInfo);

    }
    return game
  }
  async beginBefore(roomInfo) {
    let game = null;
    
    game = await this.cmmjService.beginBefore(roomInfo);
    
    return game;
  }
  async gameOver(roomInfo) {
    
    await this.cmmjService.gameOver(roomInfo);
 
  }

  async joinRoom(_roomId, role = 'real') {
    console.log('加入房间');
    var nickName = Math.floor(Math.random() * 10000) + '';

    var data = {
      nickName: 'test' + nickName,
      name: 'test' + nickName,
      userId: nickName,
      role: role,
      roomId: _roomId,
      roomName: nickName,
    };

    let posting:any = await this.webSocketService.joinRoomAction(data);
    console.log('rebot room:', JSON.stringify(posting));
    return posting;
  }

  async chupai({userId,roomId},i=2){
    var data = {
      userId: userId,
      roomId: roomId,
    };
    try {
    let posting:any = await this.mjService.autoChupaiAction(data);
    } catch (error) {
      console.log(error)

    }
  }
  
  async chiAction({userId,roomId},i=2){
    console.log("机器吃了");
     
    var data:any = {
      userId: userId,
      roomId:roomId,

    };
    let game: any = await this.redisService.hget('game' + roomId);
    if(game){
      var seatData = game.gameSeats.find((item) => item.userId == userId);
      if(seatData.tingMap.length>0){
        await this.guo(data);
        return
      }
    }
   
    var isChi =Math.floor(Math.random()*10)>5
    try {
      if(isChi){
        let posting:any = await this.mjService.chiAction(data);

      }else{
        await this.guo(data);

      }
    } catch (error) {
      console.log(error)

    }
  
  }
  async peng({userId,roomId},i=2){
    console.log("机器碰了");
    var data = {
      userId: userId,
      roomId:roomId,
    };
    let game: any = await this.redisService.hget('game' + roomId);
    if(game){
      var seatData = game.gameSeats.find((item) => item.userId == userId);
      if(seatData.tingMap.length>0){
        await this.guo(data);
        return
      }
    }
   
    try {
        let posting:any = await this.mjService.pengAction(data);
    } catch (error) {
      console.log(error)

    }
  
  }
  async gang({userId,roomId,pai},i=2){
    console.log("机器杠了");
    var data = {
      userId: userId,
      roomId:roomId,
      pai
    };
    let game: any = await this.redisService.hget('game' + roomId);
    if(game){
      var seatData = game.gameSeats.find((item) => item.userId == userId);
      if(seatData.tingMap.length>0){
        await this.guo(data);
        return
      }
    }
   
    try {
      let posting:any = await this.mjService.gangAction(data);
    } catch (error) {
      console.log(error)

    }
  }
  async hu({userId,roomId},i=2){
    console.log("机器糊了");
   
    var data = {
      userId: userId,
      roomId:roomId,
    };
   
    try {
      let posting:any = await this.mjService.huAction(data);
      
    } catch (error) {
     console.log(error)
    }
   
  
  }
  async guo({userId,roomId}){
    console.log("机器过了");
   
    var data = {
      userId: userId,
      roomId:roomId,
    };
   
    let posting:any = await this.mjService.guoAction(data);
  }

  async reconnection(game: any, data: any) {
    try {
      if(game.model==1){
        await this.cmmjService.reconnection(game, data);
      }else if(game.model==2){
        await this.hzmjService.reconnection(game, data);
      }
    } catch (error) {
      console.log(error);
    }
  }
}
